/*----------------------------------------------------------------------------------------------------
** Created by        : zhaoyang
** Created date      : 2022-08-10
** Version           : 1.0
** Descriptions      : W25Q128 SPI Flash驱动程序头文件
**---------------------------------------------------------------------------------------------------*/
#ifndef __W25Q128_H
#define __W25Q128_H
#include <stdint.h>
#include <board_cfg.h>


/*****************************************************************************
**
*****************************************************************************/

#define    SPIFlash_CS_LOW    nrf_gpio_pin_clear(SPI_SS_PIN)   //片选输出低电平：使能芯片
#define    SPIFlash_CS_HIGH   nrf_gpio_pin_set(SPI_SS_PIN)     //片选输出高电平：取消片选

//W25Q128 ID
#define  W25Q128_ID     0XEF17

//SPI Flash命令定义
#define    SPIFlash_WriteEnable         0x06  //写使能命令
#define    SPIFlash_WriteDisable        0x04  //写禁止命令
#define    SPIFlash_PageProgram         0x02  //页写入命令
#define    SPIFlash_ReadStatusReg       0x05  //读状态寄存器1
#define    SPIFlash_WriteStatusReg		0x01  //写状态寄存器1
#define    SPIFlash_ReadData            0x03  //读数据命令
#define    SPIFlash_SecErase            0x20  //扇区擦除
#define    SPIFlash_BlockErase          0xD8  //块擦除 //64k
#define    SPIFlash_ChipErase           0xC7  //全片擦除
#define    SPIFlash_ReadID              0x90  //读取ID
#define    SPIFlash_PowerDown           0xB9  //power down 1ua
#define    SPIFlash_Release_PowerDown   0xAB  //Release_PowerDown

#define    SPIFLASH_CMD_LENGTH          0x04
#define    SPIFLASH_WRITE_BUSYBIT       0x01

#define    SPIFlash_PAGE_SIZE           256
#define    SPIFlash_SECTOR_SIZE         (1024*4)
#define    SPIFlash_BLOCK_SIZE          (1024*64)
#define    SPI_TXRX_MAX_LEN             255

//#define    FLASH_BLOCK_NUMBLE           7
//#define    FLASH_PAGE_NUMBLE            8

void SPI_Flash_Init(void);

void SpiFlash_power_down(void);
void Flash_release_power_down(void);
void Flash_Erase_Sector(uint32_t SecAddr);
void SPIFlash_Erase_Block(uint32_t SecAddr);
void SPIFlash_Erase_Chip(void);
uint8_t Flash_Write(uint8_t *pBuffer, uint32_t WriteAddr, uint32_t WriteBytesNum);
uint8_t Flash_Read(uint8_t *pBuffer,uint32_t ReadAddr,uint32_t size);
uint16_t Flash_ReadID(void);
extern void SPI_Flash_Deinit(void);


#endif
